package application;

import java.util.ArrayList;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;

public class Faction {
	
	public String Name;
	public String ShortName;
	public String Description;
	public String Role;
	public String Affilation;
	public ArrayList<String> characters;
	
	public Faction (String Name, 
				String ShortName, String Description, 
				String Role, String Affilation, ArrayList<String> characters){
		super();
		this.Name = Name;
		this.ShortName = ShortName;
		this.Description = Description;
		this.Role = Role;
		this.Affilation = Affilation;
		this.characters = characters;
	}
	
	public Faction(){
		super();
		this.Name = "new faction";
		this.ShortName = "SNAFU";
		this.Description = "this may become a new faction";
		this.Role = null;
		this.Affilation = null;
		this.characters = new ArrayList<String>();
	}
	
	public void dbCreateFaction (String dbFile){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{

	        		db.store(this);


		}
		finally{
			db.close();
		}
	}
	
	public void dbEditFaction (String dbFile, Faction c){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{
			try{
				ObjectSet<Faction> result = db
		        .queryByExample(Faction.this);
				Faction found = (Faction) result.next();
				db.delete(found);
				this.setAffilation(c.getAffilation());
				this.setCharacters(c.getCharacters());
				this.setDescription(c.getDescription());
				this.setName(c.getName());
				this.setRole(c.getRole());
				this.setShortName(c.getShortName());
				
				db.store(this);
				
			}
			catch(IllegalStateException e){
				
			}

		}
		finally{
			db.close();
		}
	}
	
	public void dbDeleteFaction (String dbFile){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{
			try{
				ObjectSet<Faction> result = db
		        .queryByExample(Faction.this);
				Faction found = (Faction) result.next();
				db.delete(found);
			}
			catch(IllegalStateException e){
				
			}

		}
		finally{
			db.close();
		}
	}

	public String toString() {
		return Name;
	}

	public String getName() {
		return Name;
	}

	public String getShortName() {
		return ShortName;
	}

	public String getDescription() {
		return Description;
	}

	public String getRole() {
		return Role;
	}

	public String getAffilation() {
		return Affilation;
	}

	public void setName(String name) {
		Name = name;
	}

	public void setShortName(String shortName) {
		ShortName = shortName;
	}

	public void setDescription(String description) {
		Description = description;
	}

	public void setRole(String role) {
		Role = role;
	}

	public void setAffilation(String affilation) {
		Affilation = affilation;
	}

	public ArrayList<String> getCharacters() {
		return characters;
	}

	public void setCharacters(ArrayList<String> characters) {
		this.characters = characters;
	}

	
	

	
}
